package vn.edu.uit.service.fetchers;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import vn.edu.uit.service.UITAPIContant;
import vn.edu.uit.service.UITHttpRequest;
import vn.edu.uit.service.UITHttpRequest.RequestMethod;
import vn.edu.uit.service.UITService;
import android.util.Log;

public class HotelFavoriteFetcher {
    private static final String LOG_TAG = "HotelFavoriteFetcher";
    private static UITHttpRequest httpRequest;
    
    /**
     * The method to get the favorite hotel list
     * @return If success, return a hotel id array. If fail, return null.
     */
    public static ArrayList<String> getHotelFavorite() {
        UITService.REQUEST_SUCCESS = false;
        httpRequest = new UITHttpRequest(UITAPIContant.API_REST_DOMAIN_URL + UITAPIContant.Api_Functions.GET_HOTEL_FAVORITE);
        httpRequest.addParam(UITService.TAG_AUTHENTICATE, UITService.TOKEN);
        try {
            JSONObject object = httpRequest.doExecute(RequestMethod.POST);
            Log.d("JSON", object.toString());
            if(object.getInt(UITService.TAG_ERROR)==0) {        //request was successful, return a district arraylist
                UITService.REQUEST_SUCCESS = true;
                JSONArray jArray = object.getJSONArray(UITService.TAG_DATA);
                ArrayList<String> favorite = new ArrayList<String>();
                for(int i = 0; i<jArray.length(); i++){
                    favorite.add(jArray.getString(i));
                }
                return favorite;   
            } else {                                 //request was failed, return null
                UITService.REQUEST_SUCCESS = false;
                UITService.MSG = object.getString(UITService.TAG_MSG);
                return null;
            }   
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(LOG_TAG, e.getMessage());
        }
        return null;
    }
}
